package org.example.fronted.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.example.entity.Goods;
import org.example.entity.User;
import org.example.fronted.service.GoodService;
import org.example.fronted.utils.MapUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.*;

@CrossOrigin("*")
@Controller
@RequestMapping("/good")
public class GoodController {
    @Resource
    private GoodService goodService;

    @ResponseBody
    @GetMapping("/showGood")
    public Object getAllGoods(HttpSession session){

        //        模拟登陆状态，在Session中存入user
//        if(session.getAttribute("user") == null || "".equals(session.getAttribute("user"))){
//            User user = new User();
//            user.setUName("ssx");
//            user.setUId(28);
//            session.setAttribute("user",user);
//        }
        Map<String,Object> map=null;
        try {
            List<Goods> goods = goodService.selectAllGoods();
            map = MapUtils.getSuccessMap(goods);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("控制层异常");
        }
        return map;
    }



    @ResponseBody
    @PostMapping
    public Object selectGoodById(String IDList){
        System.out.println("IDList="+IDList);
        HashMap hashMap = JSON.parseObject(IDList,HashMap.class);

        Set<String> set = hashMap.keySet();


        ArrayList<Integer> arrayList = new ArrayList<>();

        Iterator<String> iterator = set.iterator();
        while (iterator.hasNext()){
            arrayList.add(Integer.valueOf(iterator.next()));
        }
        List<Goods> goodsByIdList = goodService.findGoodsByIdList(arrayList);
        System.out.println("goodsByIdList="+goodsByIdList);
        return goodsByIdList;
    }
}
